<?php

class DishesModel extends Model {
	
	/**
	 * 根据餐厅信息，计算餐厅的配送时间以及服务费
	 */
	public function getTime($detail,$list){
		
		$config = F('Sysconfig');
		//$juli   = $config['sys'];
		$query  = addressJsonDecode();
		$juli   = $config['sys'][$query['encity']];
		//$xishu  = $config['cfg']['xishu'];
		$xishu  = 1;
		$time   = 0;
		
		foreach($juli as $m=>$n){
			foreach($list as $k=>$v){
				
				//获取是否减免服务费以及服务费
				$exempt = D("Restaurant")->getDelivery($detail,$n[3]);
				$isgao  = D("Restaurant")->getOutletGao($v['detail'],$config); //判断是否属于高峰时段       
				
				if($v['outletid']==$detail['id']){
					if($n[1]){
						if($n[0]<$v['distance']&&$v['distance']<$n[1]){
							//$time = ($n[2]*2+$v['detail']['trantime'])*$xishu;
							if($v['detail']['is_yanshi']==1){
								$time = ($n[2]+$v['detail']['yanshi'])*$xishu;
							}else{
								$time = $n[2]*$xishu;
							}

							$time = $isgao['isgao']==1?$time+$isgao['gaotime']:$time;
							$time = D("Restaurant")->getIntTime($time);
							$detail['exempt'] = $exempt['exempt'];
							$detail['sever']  = $exempt['sever'];
						}
					}else{
						if($n[0]<$v['distance']){
							//$time = ($n[2]*2+$v['detail']['trantime'])*$xishu;
							if($v['detail']['is_yanshi']==1){
								$time = ($n[2]+$v['detail']['yanshi'])*$xishu;
							}else{
								$time = $n[2]*$xishu;
							}

							$time = $isgao['isgao']==1?$time+$isgao['gaotime']:$time;
							$time = D("Restaurant")->getIntTime($time);
							$detail['exempt'] = $exempt['exempt'];
							$detail['sever']  = $exempt['sever'];
						}
					}
					$detail['time'] = $time;
				}
			}
		}
		return $detail;
	}
	
	/**
	 * 获取城市的酒水列表
	 */
	public function getCityDrink($cityid){
		
		$map['city']  = $cityid;
		$map['isdel'] = 2;
		$data = M('Drink')->where($map)->order("sort desc")->select();
		
		return $data;
	}
	
	/**
	 * 获取餐厅的菜品分类菜品数量统计
	 * @param unknown_type $sortidArr
	 * @param unknown_type $list
	 * @return multitype:unknown mixed
	 */
	public function getCuisineType($sortidArr,$list){
		//移除数组中值相同的元素
		$sortid['sortid'] = array_unique($sortidArr);
			
		//获取餐厅的菜品分类菜品数量统计
		$data = sendRest("RMS/Product/bySortid", $sortid);
		
		$sortArr = $data['data'];
		foreach($sortArr as $m=>$n){
			foreach ($list as $h=>$j){
				if($j['sortid']==$n['sort']['id']){
					$dishSort[$m]['dish'][] = $j;
				}
			}
			$dishSort[$m]['sort'] = $n;
		}
		return array("data"=>$sortArr,"dishSort"=>$dishSort);
	}
	
	//获取推荐的前10条数据
	public function getTopTen($map){
		$json = sendRest("RMS/Product/topTen",$map);
		return $json['data'];
	}
	
	public function getDrinkSort($typeArr,$drink){
		//移除数组中值相同的元素
		$type['type'] = array_unique($typeArr);
		
		//获取城市的饮品分类饮品数量统计
		$typeCount = sendRest("RMS/Product/drinkbyType", $type,'post');
		
		foreach($typeCount['data'] as $m=>$n){
			foreach ($drink as $h=>$j){
				if($j['type']==$n['type'][3]){
					$drinkSort[$m]['drink'][] = $j;
				}
			}
			$drinkSort[$m]['sort'] = $n;
		}
		return $drinkSort;
	}

	//
	public function setCityDrink($drink){
		//$data = getDictByKey("Dict_drink");
		
		//饮品分类
		$map['key'] = 'drink';
		$map['pid'] = array('neq',0);
		$field = "id,key,pid,value,sort";
		$data  = M('Dict')->where($map)->order("sort desc")->getField($field,true);

		//dump($data);exit;
		$arr  = array();
		foreach($data as $k=>$v){
			foreach($drink as $m=>$n){
				if($n['type'] == $k){
					$arr[$k]['title']    = json_decode($v['value'],true);
					$arr[$k]['list'][$m] = $n;
				}
			}
		}
		return $arr;
	}
	
}
?>